﻿/*
 *  Copyright (C) 2010 by Benjamin Nolmans <benjamin.nolmans@gmail.com>
 *  and Barbara Post <postb99@gmail.com>
 *  Copyright (C) 2010 by Barbara Post postb99@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System;
using System.Xml;
using OsamesFrameworkCore.Logging;
using OsamesFrameworkCore.Xml.Types;

namespace OsamesFrameworkCore.Xml
{
    public static class XmlConfigurationLoader
    {
        private static XmlDictionaryContainer _xdcDictionaryContainer;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlConfigurationFile_"></param>
        /// <param name="xdcDictionaryContainer_"></param>
        /// <returns></returns>
        public static bool ProcessConfigurationFile(string xmlConfigurationFile_, out XmlDictionaryContainer xdcDictionaryContainer_ )
        {
            _xdcDictionaryContainer = new XmlDictionaryContainer();
            xdcDictionaryContainer_ = _xdcDictionaryContainer;

            if (!System.IO.File.Exists(@xmlConfigurationFile_))
            {
                LoggerService.LogToOutput("File '" + @xmlConfigurationFile_ + "' doesn't exists");
                return false; 
            }

            try
            {
                ProcessXmlConfigurationFile(@xmlConfigurationFile_);
                return true;
            }
            catch(Exception e)
            {
                LoggerService.LogToOutput(e.Message + " " + e.StackTrace);
                LoggerService.Log(typeof(XmlConfigurationLoader),LogLevel.Error,e.Message);
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlConfigurationFile_"></param>
        private static void ProcessXmlConfigurationFile(string xmlConfigurationFile_)
        {
            XmlDocument xdXmlDocument = new XmlDocument();

            xdXmlDocument.Load(@xmlConfigurationFile_);

            foreach (XmlNode e in xdXmlDocument.DocumentElement.ChildNodes)
            {
                //LoggerService.LogToOutput("XML node name :" + e.Name);
                ProcessNode(e, e.Name);                
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="node_"></param>
        /// <param name="strSectionName_"></param>
        private static void ProcessNode(XmlNode node_, string strSectionName_)
        {
            if (node_.Attributes != null && node_.Attributes["value"] != null && !string.IsNullOrEmpty(node_.Attributes["value"].Value))
            {
                _xdcDictionaryContainer.Add(strSectionName_, node_.Name, node_.Attributes["value"].Value);
                //LoggerService.LogToOutput(string.Format(node_.Name + " Valeur : " + node_.Attributes["value"].Value));
            }

            foreach (XmlNode i in node_.ChildNodes)
            {
                ProcessNode(i, strSectionName_);
            }
        }
    }
}
